package com.framework.utiles;

import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.util.encoders.Hex;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @Description: SHA256文件加密工具类
 * @Author: JamesYao
 * @DateTime: 2024/1/17 22:31
 */

@Slf4j
@Component
public class FileHashed {
    public String calculateSha256(MultipartFile file) throws NoSuchAlgorithmException, IOException {
        String hexString = null;
            MessageDigest instance = MessageDigest.getInstance("SHA-256");
            byte[] bytes = file.getBytes();
            instance.update(bytes);
            byte[] digest = instance.digest();
            hexString = Hex.toHexString(digest);
        return hexString;
    }
}
